define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'userinfo/index',
                    add_url: '',
                    edit_url: 'userinfo/edit',
                    multi_url: '',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'userId',
                sortName: 'userId',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'userId', title: __('UserId'),operate:false},
                        {field: 'phone', title: __('Phone')},
                        {field: 'status', title: __('Status'), formatter: Controller.api.formatter.status,operate:false},
                        {field: 'idCard', title: __('IdCard'),operate:false},
                        {field: 'name', title: __('Name'),operate: 'LIKE %...%', placeholder: '模糊搜索，*表示任意字符', style: 'width:200px'},
                        {field: 'sex', title: __('Sex'),operate:false},
                        {field: 'email', title: __('Email'),operate:false},
                        {field: 'qq', title: __('Qq'),operate:false},
                        {field: 'mobile', title: __('Mobile'),operate:false},
                        {field: 'youji', title: __('Youji'),operate:false},
                        {field: 'suozai', title: __('Suozai'),operate:false},
                        {field: 'company', title: __('Company'),formatter: Controller.api.formatter.company,operate:false},
                        {field: 'createTime', title: __('CreateTime'), formatter: Table.api.formatter.datetime,operate: 'BETWEEN', type: 'datetime', addclass: 'datetimepicker', data: 'data-date-format="YYYY-MM-DD HH:mm:ss"'},
                        {field: 'operate', title: __('Operate'), events: Controller.api.events.operate, formatter: function (value, row, index) {
                            var detail = '<a class="btn btn-xs btn-success btn-detail">详情</a> ';
                            return detail + Table.api.formatter.operate.call(this, value, row, index, table);
                        }}
                    ]
                ],
                search: false,
                //启用普通表单搜索
                commonSearch: true,
                //可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: false
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                status: function (value, row, index) {

                    switch (value){
                        case '0': vv='未注册';break;
                        case '1': vv='已注册';break;
                        case '2': vv='已认证';break;
                        case '3': vv='已绑卡';break;
                        default: vv='已绑卡';break;
                    }
                    return vv;
                },

                company: function (value, row, index) {
                    if(value==0){
                        return "普通用户";
                    }else{
                        return '企业用户';
                    }
                },
            },
            events: {//绑定事件的方法

                operate: $.extend({
                    'click .btn-detail': function (e, value, row, index) {
                        e.stopPropagation();
                        Backend.api.open('userinfo/detail/ids/' + row['userId'], __('Detail'));
                    }
                }, Table.api.events.operate)
            }
        }
    };
    return Controller;
});